<?php 
class annonce_demande extends MX_Controller{
	public $category;
	public function index($region='',$vPage='',$page=''){
		if($region=='page'){
			$region='';
			$page=$vPage;
		}
		$title='Demandes ';
		$description='Consultez ';
		$keywords='annonces, demandes';
		$message_tous =0;
		$message_particuliers =0 ;
		$message_professionnels =0;
		
		$TopAnnonces=$annonces=array();
		if($region){			
			$regionObject=$this->model('annonce_models_region')->get_region($region);
			$title.=$regionObject->nom_region;
			if($regionObject->code_departement){
				//set session region
				$this->session->set_userdata('region', $regionObject->code_departement);
			}
			$description.=$regionObject->nom_region;
			$keywords.=$regionObject->nom_region;
			$region=$regionObject->code_departement;
		}
		

		$this->load->library('session');
		$this->session->set_userdata('type','demande');

		$chercher_annonce=$this->load->Module('annonce/chercher');
		$chercher_annonce->load_session();
		$chercher_annonce->region=$region;
		$chercher_annonce->type=$this->session->userdata('type');
		/*$chercher_annonce->category=$this->session->userdata('category_id');*/
		
		$ids_anonnces=$chercher_annonce->get_list_annonces();
		
		$message_tous =$chercher_annonce->nb;
		$message_particuliers=$chercher_annonce->nb_particuliers;
		$message_professionnels =$chercher_annonce->nb_professionnels;
		
		$description.=$message_tous.' demandes ';
		
		
		$product=$this->load->Module('product/show');
		if(!empty($ids_anonnces))
		foreach ($ids_anonnces as $id_annonce){
			$annonces[$id_annonce]=$product->get_info($id_annonce);
		}
		$message_tous.=' demandes';
		$message_particuliers.=' demandes' ;
		$message_professionnels.=' demandes';
		
		if($region){
			$title.= " région ".$region;
			$description.= " région ".$region;
		}
		if($page){
			$title.= " Page ".$page;
			$description.= " Page ".$page;
		}

		$data=array(
				'title'=>$chercher_annonce->nb.' '.$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'paging'=>$chercher_annonce->paging,
				'nb'=>$chercher_annonce->nb,
				'nb_particulier'=>$chercher_annonce->nb_particuliers,
				'nb_professionnells'=>$chercher_annonce->nb_professionnels,
				'message_tous'=>$message_tous,
				'message_particuliers'=>$message_particuliers ,
				'message_professionnels'=>$message_professionnels,
				'annonces'=>$annonces,
				'TopAnnonces'=>$TopAnnonces
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['demande']='active';
		
		$this->view(
				array('header',
					'category',
					'footer'), 
				$data); // Display the page 
	} 
	public function page($page=''){
		if($page=='')$page=0;
		self::index('','',$page);
	}
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
	
			if(!is_dir($path.$cat))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
	
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
}